/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * gridPresets.java
 *
 * Created on 2-Nov-2010, 12:15:25 PM
 */
package balony;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author Barry Young
 */
public class gridPresets extends javax.swing.JFrame {

    public Balony balony;

    /** Creates new form gridPresets */
    public gridPresets() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        presetnameLabel = new javax.swing.JLabel();
        colsLabel = new javax.swing.JLabel();
        rowsLabel = new javax.swing.JLabel();
        gridnameTextField = new javax.swing.JTextField();
        gridcolsTextField = new javax.swing.JTextField();
        gridrowsTextField = new javax.swing.JTextField();
        dyLabel = new javax.swing.JLabel();
        dxLabel = new javax.swing.JLabel();
        griddxTextField = new javax.swing.JTextField();
        griddyTextField = new javax.swing.JTextField();
        presetSaveButton = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        gridcalibrateButton = new javax.swing.JButton();
        dpiLabel = new javax.swing.JLabel();
        griddpiTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setTitle("Grid Presets");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                formComponentHidden(evt);
            }
        });

        presetnameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        presetnameLabel.setText("Name");

        colsLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        colsLabel.setText("Cols");

        rowsLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        rowsLabel.setText("Rows");

        gridnameTextField.setToolTipText("The name of the grid preset in use.");

        gridcolsTextField.setToolTipText("The number of columns in the current grid preset.");
        gridcolsTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gridcolsTextFieldActionPerformed(evt);
            }
        });

        dyLabel.setText("dy");

        dxLabel.setText("dx");

        griddxTextField.setToolTipText("The horizontal spacing of spots in the current preset (in pixels)");

        presetSaveButton.setText("Save");
        presetSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                presetSaveButtonActionPerformed(evt);
            }
        });

        jButton7.setText("Delete");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        gridcalibrateButton.setText("Calibrate");
        gridcalibrateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gridcalibrateButtonActionPerformed(evt);
            }
        });

        dpiLabel.setText("dpi");

        jButton1.setText("Done");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rowsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(gridrowsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(gridcolsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dyLabel)
                                    .addComponent(dxLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(griddyTextField)
                                    .addComponent(griddxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(colsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(presetSaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(gridcalibrateButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                            .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(presetnameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gridnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dpiLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(griddpiTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(presetnameLabel)
                    .addComponent(gridnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dpiLabel)
                    .addComponent(griddpiTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(colsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rowsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(gridcolsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addComponent(gridrowsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(griddxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dxLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(griddyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dyLabel)
                                    .addComponent(presetSaveButton)
                                    .addComponent(jButton1))))
                        .addGap(23, 23, 23))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(gridcalibrateButton)
                        .addComponent(jButton7))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gridcolsTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gridcolsTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_gridcolsTextFieldActionPerformed

    private void presetSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_presetSaveButtonActionPerformed
        doSave();
    }

    public void doSave() {
        String s = gridnameTextField.getText();
        String outFile = s + Balony.PRESET;
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
            Properties p = new Properties();
            p.setProperty("name", s);
            p.setProperty("rows", gridrowsTextField.getText());
            p.setProperty("cols", gridcolsTextField.getText());
            p.setProperty("dx", griddxTextField.getText());
            p.setProperty("dy", griddyTextField.getText());
            p.setProperty("dpi", griddpiTextField.getText());
            p.storeToXML(out, "");

            out.close();
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }

        balony.loadPresets();
        balony.gridChoicejComboBox.setSelectedItem(s);
        balony.savePrefs();
}//GEN-LAST:event_presetSaveButtonActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        String s = gridnameTextField.getText();
        File outFile = new File(s + Balony.PRESET);
        int k = JOptionPane.showOptionDialog(null,
                "Are you sure you want to delete this preset?", "Delete Preset",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
                null, null, null);
        if (k == JOptionPane.NO_OPTION) {
            return;
        } else {
            outFile.delete();
            balony.allArraysMap.remove(s);
            balony.loadPresets();
            balony.updateGridType();
        }
        balony.savePrefs();
}//GEN-LAST:event_jButton7ActionPerformed

    private void gridcalibrateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gridcalibrateButtonActionPerformed
        doCalibrate();
    }

    public void doCalibrate() {
        if (balony.stepX > 0 && balony.stepY > 0) {
            DecimalFormat f = new DecimalFormat("0.00");
            griddxTextField.setText(f.format(balony.stepX));
            griddyTextField.setText(f.format(balony.stepY));
            balony.dx = balony.stepX;
            balony.dy = balony.stepY;
        }
}//GEN-LAST:event_gridcalibrateButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
        balony.calibrated = true;
        balony.prefs.setProperty(Balony.CALIBRATED, "1");
        balony.savePrefs();
        if (balony.incalibration) {
            balony.autoGridJCheckBox.setSelected(balony.autoGridSetting);
            balony.incalibration = false;
            balony.imWin.close();
            balony.doImageLoad();
        }
    }//GEN-LAST:event_formComponentHidden

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        balony.autoGridJCheckBox.setSelected(balony.autoGridSetting);
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new gridPresets().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel colsLabel;
    private javax.swing.JLabel dpiLabel;
    private javax.swing.JLabel dxLabel;
    private javax.swing.JLabel dyLabel;
    private javax.swing.JButton gridcalibrateButton;
    public javax.swing.JTextField gridcolsTextField;
    public javax.swing.JTextField griddpiTextField;
    public javax.swing.JTextField griddxTextField;
    public javax.swing.JTextField griddyTextField;
    public javax.swing.JTextField gridnameTextField;
    public javax.swing.JTextField gridrowsTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton presetSaveButton;
    private javax.swing.JLabel presetnameLabel;
    private javax.swing.JLabel rowsLabel;
    // End of variables declaration//GEN-END:variables
}
